package common;

import org.junit.Test;

public class GetPermutation {
	private int index1 = 0;

	@Test
	public void getPermutation() {
		int n = 3, k = 3;
		StringBuilder ans = new StringBuilder();
		// 将n转换为int数组
		int[] nArr = new int[n];
		for (int i = 0; i < n; i++) {
			nArr[i] = i + 1;
		}
		int[] visited = new int[n];
		dfs(nArr, k, 0, new StringBuilder(), ans, false, visited);
	}

	private void dfs(int[] nArr, int k, int index, StringBuilder tmp, StringBuilder ans, boolean hasfound, int[] visited) {
		if (tmp.length() == nArr.length) {
			index1++;
			if (index1 == k) {
				ans = tmp;
				hasfound = true;
			}
			return;
		}
		for (int i = 0; i < nArr.length; i++) {
			if (!hasfound) {
				if (visited[i] == 1) continue;
				visited[i] = 1;
				tmp.append(nArr[i]);
				dfs(nArr, k, index, tmp, ans, hasfound,visited);
				visited[i] = 0;
				tmp.deleteCharAt(tmp.length() - 1);
			}
		}
	}
}
